package com.mobi.Survey.adapters;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobi.Survey.lib.activities.AbstractScreen;
import com.mobi.Survey.lib.activities.Messages;
import com.mobi.Survey.lib.activities.SurveyDownloadTask;
import com.mobi.Survey.lib.activities.SurveyDownloadTask.SurveyDownloadCallback;
import com.mobi.Survey.lib.activities.SurveyListScreen;
import com.mobi.Survey.lib.model.Credentials;
import com.mobi.Survey.lib.model.FavoritePojo;
import com.mobi.Survey.map.MapMarker;
import com.mobi.Survey.map.R;

public class CustomAdapterUserList extends BaseAdapter {
	private List<MapMarker> data;
	private Set<Long> favoriteSurveys = new HashSet<Long>();

	protected LayoutInflater mInflater;
	AbstractScreen screen;

	public CustomAdapterUserList(AbstractScreen ctx) {
		this.screen = ctx;
		this.mInflater = LayoutInflater.from(ctx);
	}

	public void setFavoriteSurveys(Set<Long> surveyIds) {
		this.favoriteSurveys = surveyIds;
	}

	public void setData(List<MapMarker> data) {
		this.data = data;
	}

	@Override
	public int getCount() {

		return data.size();
	}

	public List<MapMarker> getData() {
		return this.data;
	}

	@Override
	public Object getItem(int arg0) {

		return null;
	}

	@Override
	public long getItemId(int arg0) {

		return 0;
	}

	class ViewHolder {

		TextView tvDescription;
		TextView tvUserId;

		View btnMakeFavorite;
		ImageView ivSurveyIcon;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {

		final ViewHolder holder;
		final MapMarker marker = data.get(position);

		if (favoriteSurveys.contains(marker.getSurveyId())) {
			marker.setFavorite(true);
		}
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.user_list_row, null);
			holder = new ViewHolder();

			holder.tvDescription = (TextView) convertView
					.findViewById(R.id.tvDescription);

			holder.tvUserId = (TextView) convertView
					.findViewById(R.id.tvUserId);

			holder.btnMakeFavorite = convertView
					.findViewById(R.id.btnMakeFavorite);
			holder.ivSurveyIcon = (ImageView) convertView
					.findViewById(R.id.ivSurveyIcon);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}

		holder.ivSurveyIcon.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				onIconClick(marker, v);

			}
		});

		holder.tvDescription.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				onIconClick(marker, v);

			}
		});

		holder.tvUserId.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				onIconClick(marker, v);

			}
		});

		holder.btnMakeFavorite.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				marker.setFavorite(!marker.isFavorite());

				if (!marker.isFavorite()) {

					FavoritePojo.deleteBySurveyId(
							holder.btnMakeFavorite.getContext(),
							marker.getSurveyId());
					holder.btnMakeFavorite
							.setBackgroundDrawable(holder.btnMakeFavorite
									.getResources().getDrawable(
											R.drawable.favorite_inactive));
				} else {
					FavoritePojo.commit(holder.btnMakeFavorite.getContext(),
							marker.toFavoritePojo());
					holder.btnMakeFavorite
							.setBackgroundDrawable(holder.btnMakeFavorite
									.getResources().getDrawable(
											R.drawable.favorite_active));
				}

			}
		});

		if (!marker.isFavorite()) {

			holder.btnMakeFavorite.setBackgroundDrawable(holder.btnMakeFavorite
					.getResources().getDrawable(R.drawable.favorite_inactive));
		} else {

			holder.btnMakeFavorite.setBackgroundDrawable(holder.btnMakeFavorite
					.getResources().getDrawable(R.drawable.favorite_active));
		}

		String userId = marker.getUserId();
		String des = marker.getDescription();
		 
		if (null != userId) {
			holder.tvUserId.setText(userId.trim());
		}
		
		holder.tvDescription.setText((position+1)+"");
		if (null != des) {
			holder.tvDescription.append(". ");
			holder.tvDescription.append(des.trim());
		}

		if (null != marker.getIconBitmap()) {
			holder.ivSurveyIcon.setBackgroundDrawable(null);
			holder.ivSurveyIcon.setBackgroundDrawable(new BitmapDrawable(marker
					.getIconBitmap()));
		} else {
			holder.ivSurveyIcon.setBackgroundDrawable(null);
			holder.ivSurveyIcon.setBackgroundDrawable(convertView
					.getResources().getDrawable(R.drawable.ic_launcher));
		}

		return convertView;

	}

	private void onIconClick(final MapMarker marker, final View v) {

		AbstractScreen.setMapTabId(v.getContext(), 2);
		Credentials c = new Credentials(marker.getUserId(), "");
		SurveyDownloadTask task = new com.mobi.Survey.lib.activities.SurveyDownloadTask(
				v.getContext(), c);
		task.setCallback(new SurveyDownloadCallback() {

			@Override
			public void onPostExecute(int result) {
				switch (result) {
				case SurveyDownloadTask.SWITCH_SCREEN:
					screen.setUserPrefrences();
					
					Intent i = new Intent(v.getContext(), SurveyListScreen.class);
					i.putExtra("user_id", marker.getUserId());
					v.getContext().startActivity(i
							);
					break;
				case SurveyDownloadTask.LOGIN_ERROR:
					screen.showAlert("Not found");
					
					break;

				case SurveyDownloadTask.NO_INTERNET_CONNECTION:
					screen.showShortToast(Messages.NO_INTERNET);
					break;
				}

			}

			@Override
			public void inBackground() {
				screen.populateUserBitmaps(true);
			}
		});

		task.execute();

	}

}
